package com.study.talk.ui.app.friend

import android.os.Bundle
import android.view.Menu
import android.view.View
import android.widget.ArrayAdapter
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import com.study.talk.R
import com.study.talk.ui.app.base.BaseActivity
import com.study.talk.ui.app.moment.MomentsActivity
import com.study.talk.databinding.ActivityUserDetailsBinding
import com.study.talk.data.model.UserInfo
import com.study.talk.netty.message.response.AcceptResponse
import com.study.talk.netty.message.response.AddUserSelfResponse
import com.study.talk.utils.alert.AlertUtil
import com.study.talk.utils.extras.getIntParam
import com.study.talk.utils.extras.showToast
import com.study.talk.utils.extras.toMoments
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@AndroidEntryPoint
class UserDetailsActivity : BaseActivity() {

    companion object {
        private const val TAG = "UserInfoActivity"
        const val DATA_NAME = "userId"
    }


    private val binding: ActivityUserDetailsBinding by lazy {
        DataBindingUtil.setContentView(this, R.layout.activity_user_details)
    }

    private val vm by viewModels<UserDetailsViewModel>()

    private val userId by lazy { getIntParam(DATA_NAME) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setSupportActionBar(binding.userInfoToolbar)

        binding.vm = vm
        binding.lifecycleOwner = this

        vm.user.observe(this@UserDetailsActivity) {
            vm.queryIsMyFriend(it.id)
            initView(it)
        }

        vm.isFriend.observe(this) {
            updateFriendList()
        }

        vm.loadUser(userId)
    }


    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.moment, menu)
        return super.onCreateOptionsMenu(menu)
    }

    private fun initView(user: UserInfo) {

        binding.userInfoAddBt.visibility = View.VISIBLE

        binding.userInfoToolbar.setNavigationOnClickListener { finish() }
        binding.userInfoToolbar.setOnMenuItemClickListener { item ->
            if (item.itemId == R.id.user_info_moment) {
                toMoments(MomentsActivity.MOMENT_FRIEND,user.id)
            }
            true
        }

        binding.userInfoListView.adapter = ArrayAdapter(
            this@UserDetailsActivity, android.R.layout.simple_list_item_1, createListViewData(user)
        )

        binding.userInfoAddBt.setOnClickListener { _ ->
            if (vm.isFriend.value == true) {
                AlertUtil.showCustomDialog(
                    this@UserDetailsActivity, "删除好友提醒", "确定要删除好友${user.username}吗？"
                ) { if (it) vm.deleteFriend(user.id) }
            } else {
                vm.addFriendRequest(user.id)
            }
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onAddUserSelfResponseMessage(response: AddUserSelfResponse) {
        if (response.success) {
            showToast("好友请求发送成功!")
        } else {
            showToast("发送失败:${response.reason}")
        }
    }


    private fun createListViewData(user: UserInfo): List<String> {
        return listOf(
            "性别：" + (if (user.gender == 1) "男" else "女"),
            "生日：${user.birth}",
            "电话：${user.phone}",
            "邮箱：${user.email}",
            "地址：${user.address}",
            "简介：${user.introduce}"
        )
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onAddUserResponse(response: AcceptResponse) {
        if (response.invitedUserId == vm.user.value?.id) {
            if (response.success) vm.changeStatus(true)
        }
    }
}